package com.ruoyi.web.ydz.service.impl;

import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.ydz.domain.MedicationReminder;
import com.ruoyi.web.ydz.domain.Notice;
import com.ruoyi.web.ydz.domain.UserDocument;
import com.ruoyi.web.ydz.mapper.MedicationReminderMapper;
import com.ruoyi.web.ydz.mapper.NoticeMapper;
import com.ruoyi.web.ydz.mapper.UserDocumentMapper;
import com.ruoyi.web.ydz.service.INoticeService;
import com.ruoyi.web.ydz.util.websocket.WebSocketUsers;
import org.apache.poi.sl.usermodel.Notes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 系统通知Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-10
 */
@Service
public class NoticeServiceImpl implements INoticeService
{
    @Resource
    private NoticeMapper noticeMapper;

    @Resource
    private UserDocumentMapper userDocumentMapper;

    @Resource
    private MedicationReminderMapper medicationReminderMapper;

    /**
     * 查询系统通知
     * 
     * @param id 系统通知主键
     * @return 系统通知
     */
    @Override
    public Notice selectNoticeById(Long id)
    {
        return noticeMapper.selectNoticeById(id);
    }

    /**
     * 查询系统通知列表
     * 
     * @param notice 系统通知
     * @return 系统通知
     */
    @Override
    public List<Notice> selectNoticeList(Notice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增系统通知
     * 
     * @param notice 系统通知
     * @return 结果
     */
    @Override
    public int insertNotice(Notice notice)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        notice.setCreateTime(DateUtils.getNowDate());
        notice.setCreateBy(user.getUserName());
        notice.setCreateName(user.getNickName());

        /* 通知在线的全部用户*/
        if ("0".equals(notice.getStatus())){
            WebSocketUsers.sendMessageToUsersByText(JSONUtil.toJsonStr(notice));
        }

        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改系统通知
     * 
     * @param notice 系统通知
     * @return 结果
     */
    @Override
    public int updateNotice(Notice notice)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        notice.setUpdateTime(DateUtils.getNowDate());
        notice.setUpdateBy(user.getUserName());
        notice.setUpdateName(user.getNickName());

        /* 通知在线的全部用户*/
        if ("0".equals(notice.getStatus())){
            WebSocketUsers.sendMessageToUsersByText(JSONUtil.toJsonStr(notice));
        }

        return noticeMapper.updateNotice(notice);
    }

    /**
     * 批量删除系统通知
     * 
     * @param ids 需要删除的系统通知主键
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] ids)
    {
        return noticeMapper.deleteNoticeByIds(ids);
    }

    /**
     * 删除系统通知信息
     * 
     * @param id 系统通知主键
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long id)
    {
        return noticeMapper.deleteNoticeById(id);
    }

    @Override
    public AjaxResult getNotes(){
        AjaxResult ajaxResult = new AjaxResult();
        HashMap<String,Object> resultMap = new HashMap<>();

        /*获取用户疾病信息*/
        SysUser user = SecurityUtils.getLoginUser().getUser();
        UserDocument userDocument = userDocumentMapper.selectUserDocumentByUserId(user.getUserId());

        /*查询系统通知*/
        Notice notice = new Notice();
        notice.setNoticeType("0");
        notice.setCreateTime(user.getCreateTime());
        List<Notice> systemNotices = noticeMapper.selectNoticeList(notice);
        resultMap.put("systemNotices",systemNotices);

        /*查询疾病通知*/
        if (!Objects.isNull(userDocument) && !Objects.isNull(userDocument.getDiseaseId())){
            notice.setNoticeType("1");
            notice.setDisease(String.valueOf(userDocument.getDiseaseId()));
            List<Notice> diseaseNotices = noticeMapper.selectNoticeList(notice);
            resultMap.put("diseaseNotices",diseaseNotices);
        }else {
            ajaxResult.put("msg","未查询到当前用户的疾病信息");
            resultMap.put("diseaseNotices",null);
        }

        /*查询用药提醒*/
        MedicationReminder medicationReminder = new MedicationReminder();
        medicationReminder.setCreateBy(String.valueOf(user.getUserId()));
        medicationReminder.setRemindTime(DateUtils.getNowDate());
        List<MedicationReminder> medicationReminders = medicationReminderMapper.selectMedicationReminderList(medicationReminder);
        resultMap.put("medicationReminders",medicationReminders);

        ajaxResult.put("coed",200);
        ajaxResult.put("data",resultMap);
        if (Objects.isNull(ajaxResult.get("msg"))) ajaxResult.put("msg","查询成功");
        return ajaxResult;

    }
}
